package com.six.core.service.impl;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six.core.entity.Emp;
import com.six.core.service.EmpService;
import com.six.core.mapper.EmpMapper;
import com.six.core.util.JwtUtil;
import com.six.core.vo.EmpVo;
import com.six.core.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author:LiWei
 * @date:2023年02月27日10:10
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService{
    @Autowired
    private EmpMapper empMapper;

    @Override
    public Result findAll(Integer currentPage, Integer pageSize, EmpVo empVo) {
        Page<Emp> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.like(empVo.getUsername()!=null&&empVo.getUsername()!="" ,"username",empVo.getUsername());
        wrapper.ge(empVo.getStartDate()!=null&&empVo.getStartDate()!="" ,"gmt_create",empVo.getStartDate());
        wrapper.le(empVo.getEndDate()!=null&&empVo.getEndDate()!="" ,"gmt_create",empVo.getEndDate());
        Page<Emp> empPage = empMapper.selectPage(page, wrapper);
        return new Result(200,"查询成功",empPage);

    }

    @Override
    public Result deleteByEmpId(Integer id) {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int i = empMapper.delete(wrapper);
        if(i>0){
            return new Result(200,"删除成功",i);
        }else {
            return new Result(500, "删除失败", i);
        }
    }

    @Override
    public Result addEmp(Emp emp) {
        int insert = empMapper.insert(emp);
        if(insert>0){
            return new Result(200,"添加成功",null);
        }else {
            return new Result(200,"添加失败",null);
        }

    }

    @Override
    public Result updateEmp(Emp emp) {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = emp.getPassword();
        String encode = encoder.encode(password);
        emp.setPassword(encode);
        int i = empMapper.updateById(emp);

        if (i>0){
            return new Result(200,"修改成功!",null);
        }
        return new Result(500,"修改失败!",null);
    }

    @Override
    public Result updateDisable(Emp emp) {
        int i = empMapper.updateById(emp);
        if (i>0){
            return new Result(2000,"修改状态成功!",null);
        }
        return new Result(5000,"修改状态失败!",null);
    }
}




